<?php

namespace Home\Controller;

use Home\Org\Util\GtBuyer;

class ProjectController extends HomeController{

    public function index(){
        exit();
    }

    public function add(){
        $this->display();
    }

    public function save(){

        // 准备数据
        $respons = array('code'=>0,'msg'=>'操作成功!');

        $type = I('type');
        $cp   = I('cp');
        $remarkes = I('remarkes');

        $model = D("project");

        $user = session('user_name');
        $name = $user.'-P-'.time();
        $stock_process = array();
        $stock_process[] =$cp;
        // <- 以上基本数据准备完成.

        // 审批流判断开始 ->
        $runProcessTag = 1; // 0 : 不需要审批(默认), 1 : 要审批
        $items = array();
        $nowTime = date('Y-m-d H:i:s');
        $yesterdayTime = date('Y-m-d H:i:s',strtotime('-1 day'));

        // 审批流公共部分
        import("@.ORG.Util.GtBuyer");
        $gtBuyer = GtBuyer::getInstance();
        // 根据提交的$cp查找经理，并拼接审批流
        $theCaptain = $gtBuyer->getAllCaptain($cp,1);
        if($theCaptain){
            $stock_process[] = $theCaptain;
        }

        // 审批流特殊部分，拼接特殊人物：总监
        $theCaptain = $gtBuyer->getAllCaptain($cp,2);
        if($theCaptain){
            $stock_process[] = $theCaptain;
        }

        // 保存数据
        $stData = array('name' => $name, 'ctime' => $nowTime, 'user' => $user, 'to_user' => $stock_process[0], 'step' => 0, 'remarkes' => $remarkes, 'type' => $type, 'stock_process' => json_encode($stock_process, JSON_UNESCAPED_UNICODE), 'manager' => $cp);
        $id = $model->add($stData);
        $this->_saveLog(array('stocking_id' => $id, 'user' => $user, 'info' => '创建'));

        echo json_encode($respons);

    }

    public function myprojects(){

        $key = I('keyword');
        $type = I('eq_type');
        $map=array();
        $user  = session('user_name');
        if($type=='-8'){
            $map['concat(stock_process,user)'] = array('like',  '%' . trim($user) . '%');
        }
        else
            $map['concat(to_user,user)'] = array('like',  '%' . trim($user) . '%');

        if(isset($_POST['type'])||$type){
            if($type!='-8')
                $map['status'] = array('eq', $type);
        }else{
            $map['status'] = array('eq', 1);
        }

        if($key){
            $map['concat(name)'] = array('like',  '%' . trim($key) . '%');
        }
        $model = M("Project");
        $this->assign('curr_user',session('user_name'));
        $this->assign('keyword',$key);
        $this->assign('status',$type?:1);
        $res= $this -> _list($model, $map, 'id desc');
        $this->display();

    }

    public function delete(){
        $id = (int)I('id');
        $res =  D('Project')->where("id={$id}")->delete();
        $res+=  D('ProjectLog')->where("pid={$id}")->delete();
        echo $res;exit;
    }

    public function info(){
        $id = (int)I('id');
        $info = D('Project')->where("id={$id}")->find();
        if($info){

            if(strtolower(session('user_name'))==strtolower($info['to_user'])){
                $this->assign('access',true);
            }

            if($info['status']==2||$info['status']==-1){
                $this->assign('access',false);
            }
        }
        $this->assign('log',D('ProjectLog')->where("pid={$id} and type='project'")->select());
        $this->assign('info',$info);
        $this->display();
    }

    public function review(){
        $respons = array('code'=>0,'msg'=>'');
        $id = I('id');
        $status = I('status');
        $model =  D('Project');
        $info =$model->where("id={$id}")->find();
        $reviewstep = json_decode($info['stock_process'],true);
        if($info['step']==count($reviewstep)-1){ //完结 最后一个审核
            if($status!=-1)
                $status = 2;
            $info['to_user'] = '';

        }else{
            $info['to_user'] = $reviewstep[$info['step']+1];
        }

        if($status==-1){
            $info['to_user'] = '';
        }
        $info['status'] = $status;
        $info['step']+=1; //当前审核步骤+1
        $remarkes = I('remarkes');
        $loginfo = $info['status']>=1?"同意":"否决";
        $this->_saveLog(array('stocking_id'=>$id,'user'=>session('user_name'),'info'=>$remarkes?$loginfo."  备注:".$remarkes:$loginfo));
        if($info['status']!=-1&&$info['status'] != 2&&$info['to_user']){
//            $this->sendEmail($info['to_user'].'@g-town.com.cn', "有新的项目申请单等待您审核", "申请人:{$info['user']}");
        }
        if($status==-1) {
            // 不通过 : 向之前所有已审批通过的用户发送邮件通知：此单被XXX拒绝，拒绝理由：YYY
            $currentOperator = session('emp_no');
            foreach ($reviewstep as $key => $item)
            {
                if($item == $currentOperator)
                {
                    break;
                }else{
                    //$userName = "maxwell.shi";
//                    $this->sendEmail($item.'@g-town.com.cn', "项目申请单{$info['name']}未通过审核", "申请人:{$info['user']}<br/>未通过原因:{$remarkes}");
                }
            }
        }

        $model->where("id={$id}")->save($info);
        echo json_encode($respons);exit;

    }

    function _saveLog($data = array()){
        return D('ProjectLog')->add(array('pid'=>$data['stocking_id'],'user'=>$data['user'],'info'=>$data['info'],'ctime'=>date('Y-m-d H:i:s'),'type'=>'project'));
    }

}